package com.yzh.homework;

import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author 于振晗
 * @title: Test
 * @description: TODO 主方法
 * @date 2020年09月02日
 * @path com.yzh.homework
 */
public class Test {
    private static Set<Product> products = new TreeSet<Product>();
    private static Scanner sc = new Scanner(System.in);

    static {

    }

    public static void main(String[] args) {
        do {
            System.out.println();
            System.out.println("****欢迎进入京东商城****");
            System.out.println("\t1.添加商品");
            System.out.println("\t2.查看所有商品");
            System.out.println("\t3.查看指定编号商品");
            System.out.println("\t4.添加到购物车");
            System.out.println("\t5.显示购物车");
            System.out.println("\t6.退出");
            System.out.println("***********************");
            System.out.print("请选择菜单: ");
            int sel = sc.nextInt();
            //选择
            switch (sel) {
                case 1:
                    addProduct();
                    break;
                case 2:
                    showAllProduct();
                    break;
                case 3:
                    System.out.println("\t3.查看指定编号商品");
                    System.out.println("请输入指定编号");
                    int id1 = sc.nextInt();
                    for (Product product : products){
                        if (product.getId() == id1){
                            System.out.println(product);
                            break;
                        }
                    }
                    break;
                case 4:
                    System.out.println("\t4.添加到购物车");
                    break;
                case 5:
                    System.out.println("\t5.显示购物车");
                    break;
                case 6:
                    System.out.println("\t6.退出");
                    System.out.println("系统退出");
                    break;
                default:
                    System.out.println("输入错误");
                    break;
            }
        } while (true);

    }

    public static void addProduct(){
        System.out.println("\t1.添加商品");
        System.out.println("请输入商品id");
        int id = sc.nextInt();
        System.out.println("请输入商品名称");
        String name = sc.next();
        System.out.println("请输入商品价格");
        double price = sc.nextDouble();
        System.out.println("请输入商品颜色");
        String color = sc.next();
        System.out.println("请输入商品尺寸");
        int size = sc.nextInt();
        System.out.println("请输入商品库存");
        int stock = sc.nextInt();
        Product product = new Product(id, name, price, color, size, stock);
        products.add(product);
    }

    public static void showAllProduct(){
        System.out.println("\t2.查看所有商品");
        products.forEach((item) -> System.out.println(item));
    }
}

//商品类
class Product implements Comparable<Product> {
    private int id = 0;
    private String name = null;
    private double price;
    private String color;
    private int size;
    private int stock;

    public Product(int id, String name, double price, String color, int size, int stock) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.color = color;
        this.size = size;
        this.stock = stock;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getStock() {
        return stock;
    }

    public void setStock(int stock) {
        this.stock = stock;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Product product = (Product) o;

        if (id != product.id) return false;
        if (Double.compare(product.price, price) != 0) return false;
        if (size != product.size) return false;
        if (stock != product.stock) return false;
        if (name != null ? !name.equals(product.name) : product.name != null) return false;
        return color != null ? color.equals(product.color) : product.color == null;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = id;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        temp = Double.doubleToLongBits(price);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        result = 31 * result + (color != null ? color.hashCode() : 0);
        result = 31 * result + size;
        result = 31 * result + stock;
        return result;
    }


    @Override
    public int compareTo(Product product) {

        if (id - product.id == 0) {
            System.out.println("添加失败,原因,商品id不可重复");
            return 0;
        }

        if (name.compareTo(product.name) == 0) {
            System.out.println("添加失败,原因,商品名称不可重复");
            return 0;
        }
        return id - product.id;
    }

    @Override
    public String toString() {
        return "" +
                "商品编号为:" + id +
                ", 商品名称为:" + name +
                ", 价格为:" + price +
                ", 颜色为:" + color +
                ", 尺寸为:" + size +
                ", 库存为:" + stock;
    }
}
